package com.siyao.redvip.ui.activity;

import java.util.HashMap;
import java.util.Map;
import com.android.volley.http.RequestManager;
import com.android.volley.http.RequestManager.RequestListener;
import com.google.gson.reflect.TypeToken;
import com.siyao.redvip.R;
import com.siyao.redvip.RedApplication;
import com.siyao.redvip.bean.TResponseBean;
import com.siyao.redvip.components.base.BaseFragmentActivity;
import com.siyao.redvip.network.NetConstans;
import com.siyao.redvip.utils.SharedPreferencesUtils;
import com.siyao.redvip.utils.ToastUtils;
import com.siyao.redvip.utils.Track;
import com.siyao.redvip.volley.NetBean;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
 * ForgetPwdActivity
 * 
 * @author lenovo
 * 
 */
public class ForgetPwdActivity extends BaseFragmentActivity {

	private Button updatePwd;
	private EditText etPasswrod, etPasswrod2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitleText("修改密码");
		setLeftButton("返回");
	}

	@Override
	protected void beforeInit() {

	}

	@Override
	public void ininlazeLayout() {
		View appLayout = getLayoutInflater().inflate(
				R.layout.activity_update_pwd, null);
		appMainView.addView(appLayout, layoutParams);
	}

	@Override
	public void ininlazeView() {
		updatePwd = (Button) findViewById(R.id.btn_update_pwd);
		etPasswrod = (EditText) findViewById(R.id.etPasswrod);
		etPasswrod2 = (EditText) findViewById(R.id.etPasswrod2);
		updatePwd.setOnClickListener(this);
	}

	@Override
	public void ininlazeData() {

	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		int id = v.getId();
		switch (id) {
		case R.id.btn_update_pwd:
			doUpdatePWD();
			break;

		default:
			break;
		}
	}

	public void doUpdatePWD() {
		showPd();
		final String pwd1 = etPasswrod.getText().toString();
		String pwd2 = etPasswrod2.getText().toString();
		if (pwd1.length() == 0 | pwd2.length() == 0) {
			ToastUtils.makeMesageLong(this, "新密码必须填写！");
			return;
		}

		if (!pwd1.equals(pwd2)) {
			ToastUtils.makeMesageLong(this, "仔细检查密码是否输入有误！");
			return;
		}

		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("loginName", RedApplication.getUser().getVolunteerId());
		paramMap.put("password", pwd1);

		RequestManager.getInstance(this).post(NetConstans.UPDATE_PWD, paramMap,
				new RequestListener() {

					@Override
					public void onSuccess(String response, String url,
							int actionId) {
						dismissPd();
						Track.i(response);
						NetBean netBean=gson.fromJson(response, new TypeToken<NetBean>() {}.getType());
						if (netBean.getStatus() == 1) {
							SharedPreferencesUtils.savePasswd(
							ForgetPwdActivity.this, pwd1);
							ToastUtils.makeMesageLong(ForgetPwdActivity.this, netBean.getMessage());
							Intent intent = new Intent();
							intent.setClass(ForgetPwdActivity.this, LoginActivity.class);
							startActivity(intent);
							finish();
						}else{
							ToastUtils.makeMesageLong(ForgetPwdActivity.this, netBean.getMessage());
						}
					}

					@Override
					public void onRequest() {

					}

					@Override
					public void onError(String errorMsg, String url,
							int actionId) {

					}
				}, 2);

	}
}
